package com.zf.shuashua.ui.mine.notice;

import android.content.Intent;
import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.MessageLoveList;
import com.zf.shuashua.bean.VideoInfo;
import com.zf.shuashua.utils.ToastUtil;
import com.zf.shuashua.video.VideoPlayActivity;

import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;

/**
 * 消息-点赞列表
 */
public class LoveListFragment extends BaseListFragment<MessageLoveList.DataBean> {

    private RecyclerView mRecyclerView;
    private MessageLoveListAdapter mAdapter;

    public static LoveListFragment getFragment(){
        return new LoveListFragment();
    }

    @Override
    protected void initView() {
//        setState(ViewState.EMPTY);
        setEmptyLayout(R.layout.empty_love_list);
        super.initView();
    }

    @Override
    protected void initData() {
        loadPage(1);
    }

    @Override
    protected Observable<Result<List<MessageLoveList.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getNoticeLoveList(pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<MessageLoveList.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<MessageLoveList.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    MessageLoveList list = ParseJsonUtils.parseData(s, MessageLoveList.class);
                    result.data = list.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list_search_video;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mRecyclerView = findViewById(R.id.recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ((SimpleItemAnimator) Objects.requireNonNull(mRecyclerView.getItemAnimator())).setSupportsChangeAnimations(false);
        return mRecyclerView;
    }

    @Override
    protected MeiBaseAdapter<MessageLoveList.DataBean> getAdapter() {
        mAdapter = new MessageLoveListAdapter(R.layout.item_message_love, null);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MessageLoveList.DataBean dataBean = (MessageLoveList.DataBean) adapter.getData().get(position);
            MessageLoveList.DataBean.DateBean.NewsBean news = dataBean.getData().getNews();
            if(news == null){
                return;
            }
            jumpToDetail(news.getVideo_id());

        });
        return mAdapter;
    }

    private void jumpToDetail(String video_id) {
        if(TextUtils.isEmpty(video_id)){
            return;
        }
        int videoId = Integer.parseInt(video_id);
        HttpData.getInstance().getVideoInfo(videoId, new ACallback<BaseData<VideoInfo>>() {
            @Override
            public void onSuccess(BaseData<VideoInfo> data) {
                if(data.isStatus()){
                    Intent intent = new Intent(getContext(), VideoPlayActivity.class);
                    intent.putExtra("hotdataList", data.getData());
                    mActivity.startActivity(intent);
                }else{
                    ToastUtil.showToast(getContext(), data.getMessage());
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                ToastUtil.showToast(getContext(), errMsg);
            }
        });


    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return false;
    }
}
